package com.base.cn.platform.os.controller.statistics;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import com.base.cn.platform.os.service.course.material.CouMaterialService;
import com.base.cn.platform.os.service.manage.statistics.goods.GoodsStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 访问统计Controller
 *
 * @author hackeridear
 */
@Controller
@RequestMapping("/admin/stat")
public class GoodsStatController extends AdminBaseController {

    @Autowired
    private GoodsStatisticsService goodsStatisticsService;
    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private CouMaterialService couMaterialService;

    @RequestMapping("/goods")
    public String goods(){
        return resultPage("/statistics/goods/goods-statistics");
    }


    /**
     * 商品统计接口
     * @return Map<String,Object>
     */
    @RequestMapping("/queryGoodsProfileCountData")
    @ResponseBody
    public Map<String,Object> queryGoodsProfileCountData(){
        return goodsStatisticsService.queryGoodsProfileCountData();
    }

    /**
     * 访客榜 TOP5 之 商品
     */
    @RequestMapping("/queryUVTopFiveGoodsList")
    public String queryUVTopFiveGoodsList(Model model,
                                          @RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                          @RequestParam(value = "startTime",required = false) Date startTime,
                                          @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params = new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        Map<String,Object> resultMap = goodsStatisticsService.queryUVTopFiveGoodsList(params);
        if(ResultUtil.getSuccess(resultMap)){
            model.addAttribute("topFivShopList",ResultUtil.getEntity(resultMap));
        }
        return resultPage("/statistics/goods/goods-top-fiv-shop") ;
    }

    /**
     * 支付榜 TOP5 之 付费商品
     */
    @RequestMapping("/queryGoodsPayTopFiveList")
    public String queryGoodsPayTopFiveList(Model model,
                                          @RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                          @RequestParam(value = "startTime",required = false) Date startTime,
                                          @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params = new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        Map<String,Object> resultMap = goodsStatisticsService.queryGoodsPayTopFiveList(params);
        if(ResultUtil.getSuccess(resultMap)){
            model.addAttribute("payFivShopList",ResultUtil.getEntity(resultMap));
        }
        return resultPage("/statistics/goods/goods-pay-fiv-shop") ;
    }

    /**
     * 考试榜 TOP5 之 试卷
     */
    @RequestMapping("/queryExamUseFiveList")
    public String queryExamUseFiveList(Model model,
                                           @RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                           @RequestParam(value = "startTime",required = false) Date startTime,
                                           @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params = new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        Map<String,Object> resultMap = goodsStatisticsService.queryExamUseFiveList(params);
        if(ResultUtil.getSuccess(resultMap)){
            model.addAttribute("examUseFiveList",ResultUtil.getEntity(resultMap));
        }
        return resultPage("/statistics/goods/goods-exam-use-five-list") ;
    }

    /**
     * 学习榜 TOP7之 专业/科目
     */
    @RequestMapping("/querySubjectStudyRankingList")
    public String querySubjectStudyRankingList(Model model,
                                       @RequestParam(value = "type",required = false,defaultValue = "1") int type,
                                       @RequestParam(value = "startTime",required = false) Date startTime,
                                       @RequestParam(value = "endTime",required = false) Date endTime){
        Map<String,Object> params = new HashMap<>();
        params.put("type",type);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        Map<String,Object> resultMap = goodsStatisticsService.querySubjectStudyRankingList(params);
        if(ResultUtil.getSuccess(resultMap)){
            model.addAttribute("subjectStudRnkList",ResultUtil.getEntity(resultMap));
        }
        return resultPage("/statistics/goods/goods-subject-stud-rnk-list") ;
    }



    /**
     * 课程学习走势图统计
     *
     * @param type
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/queryCourseStudyStatis")
    @ResponseBody
    public Map<String, Object> queryCourseStudyStatis(@RequestParam(value = "type", defaultValue = "2") Integer type,
                                                      @RequestParam(value = "startTime", required = false) Date startTime,
                                                      @RequestParam(value = "endTime", required = false) Date endTime,
                                                      @RequestParam("courseId") BigDecimal courseId) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return  coursePlayRecordService.queryCourseStudyStatis( courseId, params);
    }

    /**
     * 课程完成率走势图统计
     *
     * @param type
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/queryCourseFinishStatis")
    @ResponseBody
    public Map<String, Object> queryCourseFinishStatis(@RequestParam(value = "type", defaultValue = "2") Integer type,
                                                       @RequestParam(value = "startTime", required = false) Date startTime,
                                                       @RequestParam(value = "endTime", required = false) Date endTime,
                                                       @RequestParam("courseId") BigDecimal courseId) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return  coursePlayRecordService.queryCourseFinishStatis( courseId, params);
    }

    /**
     * 直播统计
     *
     * @param
     * @param courseId
     * @return
     */
    @RequestMapping("/queryLiveStatis")
    @ResponseBody
    Map<String, Object> queryLiveStatis(@RequestParam("courseId") BigDecimal courseId,
                                        @RequestParam(value = "materialId", required = false) BigDecimal materialId,
                                        @RequestParam(value = "catalogId",required = false)BigDecimal catalogId) {
        return coursePlayRecordService.queryLiveStatis(courseId,materialId,catalogId);
    }

    /**
     * 直播互动记录
     * @param pageSize   页容量
     * @param pageNum    页码
     * @return
     */
    @RequestMapping("/getInteractivePage")
    public String  getInteractivePage(Model model,
                                      @RequestBody Map<String,Object> params,
                                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                      @RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum){
        BigDecimal materialId=null;
        if (ObjectUtils.isNotEmpty(params)) {
            materialId = new BigDecimal(params.get("materialId").toString());
            Map<String,Object> materialMap = couMaterialService.findMaterialById(materialId,false,false);
            String playType = materialMap.get("playType").toString();
            model.addAttribute("playType",playType);
        }
        model.addAttribute("dataMap", coursePlayRecordService.getInteractivePage(materialId , pageSize, pageNum));
        return resultPage("/course/cou/courseTeachData/ajax_live_interactive_list");
    }


    /**
     * 打开聊天记录页面
     * @return
     */
    @RequestMapping("toRollCallList")
    public String toRollCallList(Model model,@RequestParam("materialId")BigDecimal materialId) {
        model.addAttribute("materialId", materialId);
        return resultPage("/course/cou/courseTeachData/live_rollcall_list");
    }
    /**
     * 去往聊天记录页面
     * @return
     */
    @RequestMapping("toChatMsgList")
    public String toChatMsgList(Model model,@RequestParam("materialId")BigDecimal materialId) {
        model.addAttribute("materialId", materialId);
        return resultPage("/course/cou/courseTeachData/live_chatmsg_list");
    }
    /**
     * 去往聊天记录页面
     * @return
     */
    @RequestMapping("tointeractiveList")
    public String tointeractiveList(Model model,@RequestParam("materialId")BigDecimal materialId) {
        model.addAttribute("materialId", materialId);
        return resultPage("/course/cou/courseTeachData/live_interactive_list");
    }
    /**
     * 打开课程目录页面
     * @return
     */
    @RequestMapping("toCatalogList")
    public String toCatalogList(Model model,@RequestParam("courseId")BigDecimal courseId) {
        model.addAttribute("courseId", courseId);
        return resultPage("/course/cou/courseTeachData/live_catalog_list");
    }
    /**
     * 直播聊天记录
     * @param pageSize   页容量
     * @param pageNum    页码
     * @return
     */
    @RequestMapping("/getChatMsgPage")
    public String  getChatMsgPage(Model model,
                                  @RequestBody Map<String,Object> params,
                                  @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum){
        BigDecimal materialId=null;
        if (ObjectUtils.isNotEmpty(params)) {
            materialId = new BigDecimal(params.get("materialId").toString());
            Map<String,Object> materialMap = couMaterialService.findMaterialById(materialId,false,false);
            String playType = materialMap.get("playType").toString();
            model.addAttribute("playType",playType);
        }
        model.addAttribute("dataMap", coursePlayRecordService.getChatMsgPage(materialId , pageSize, pageNum));
        return resultPage("/course/cou/courseTeachData/ajax_live_chatmsg_list");
    }


    /**
     * 直播学员签到记录
     * @param pageSize   页容量
     * @param pageNum    页码
     * @return
     */
    @RequestMapping("/getRollCallPage")
    public String  getRollCallPage(Model model,
                                   @RequestBody Map<String,Object> params,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                   @RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum){
        BigDecimal materialId=null;
        if (ObjectUtils.isNotEmpty(params)) {
            materialId = new BigDecimal(params.get("materialId").toString());
            Map<String,Object> materialMap = couMaterialService.findMaterialById(materialId,false,false);
            String playType = materialMap.get("playType").toString();
            model.addAttribute("playType",playType);
        }
        model.addAttribute("dataMap", coursePlayRecordService.getRollCallPage(materialId , pageSize, pageNum));
        return resultPage("/course/cou/courseTeachData/ajax_live_rollcall_list");
    }


    /**
     * 直播在线人数统计图
     *
     * @param type
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/queryLiveOnlineStatis")
    @ResponseBody
    public Map<String, Object> queryLiveOnlineStatis(@RequestParam(value = "type", defaultValue = "2") Integer type,
                                                     @RequestParam(value = "startTime", required = false) Date startTime,
                                                     @RequestParam(value = "endTime", required = false) Date endTime,
                                                     @RequestParam("courseId") BigDecimal courseId) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return coursePlayRecordService.queryLiveOnlineStatis( courseId, params);
    }
}
